package main

import (
	"fmt"
	consul "github.com/go-kratos/consul/registry"
	"github.com/go-kratos/kratos/v2"
	"github.com/go-kratos/kratos/v2/config"
	"github.com/go-kratos/kratos/v2/config/file"
	"github.com/go-kratos/kratos/v2/log"
	"github.com/go-kratos/kratos/v2/middleware/tracing"
	"github.com/go-kratos/kratos/v2/transport/grpc"
	"github.com/go-kratos/kratos/v2/transport/http"
	"github.com/google/uuid"
	"github.com/hashicorp/consul/api"
	"go_private_im/internal/comet/conf"
	"go_private_im/pkg/conversion"
	"go_private_im/third_party/zaplogger"
)

var (

	id = uuid.New().String()
)

const (
	version = "2.0.0"
	appid   = "private.comet"
)

func NewConsul(config2 *conf.Config) *api.Client {
	config := api.DefaultConfig()
	config.Address = config2.Consul.Addr
	client, err := api.NewClient(config)
	if err != nil {
		panic(err)
	}
	return client
}

func newApp(hs *http.Server,gs *grpc.Server,client *api.Client) *kratos.App {
	return kratos.New(
		kratos.ID(id),
		kratos.Name(appid),
		kratos.Version(version),
		kratos.Metadata(map[string]string{}),
		kratos.Registrar(consul.New(client)),
		kratos.Server(
			hs,
			gs,
		),
	)
}

func main() {
	c := config.New(config.WithSource(file.NewSource("configs/comet.yaml")), config.WithResolver(conversion.YamlTimeToInt))
	if err := c.Load(); err != nil {
		panic(err)
	}
	// 加载配置源：
	v := conf.Config{}
	if err := c.Scan(&v); err != nil {
		panic(err)
	}

	fmt.Println(v.Consul)
	zapLogger := log.With(zaplogger.NewZapLogger(v.Log.Path),
		"ts", log.DefaultTimestamp,
		"caller", log.DefaultCaller,
		"service.id", id,
		"service.name", appid,
		"service.version", 1,
		"trace_id", tracing.TraceID(),
		"span_id", tracing.SpanID(),
	)
	fmt.Println(zapLogger)
	app := initApp(&v, zapLogger)

	if err := app.Run(); err != nil {
		panic(err)
	}
}
